﻿# -*- coding: utf-8 -*-
'''
Created on 2012-11-29

@author: cc
'''
import os
import urllib2
import re
import gzip
import zlib
import StringIO
def info(obj,showDoc=False):
    print "class type:",type(obj)
    t=dir(obj)
    print t
    for k in t:
        att=getattr(obj, k)
        if callable(att):
            print "******************",k
            if  showDoc:
                print att.__doc__
class __decoder:
    def NoneDecoder(self,buf):
        return buf 
    def gzipDecoder(self,buf):
        io1=StringIO.StringIO(buf)
        return gzip.GzipFile(fileobj=io1).read()
    def deflateDecoder(self,buf):
        return zlib.decompressobj(-zlib.MAX_WBITS).decompress(buf)
 
def getHtml(url):
    req=urllib2.Request(url)
    req.add_header("Accept-Encoding", "gzip")
    h=urllib2.HTTPHandler(debuglevel=1)
    opener=urllib2.build_opener(h)
    file1=opener.open(req)
    s=file1.info().__str__()
    contentEncode=file1.info().getheader("Content-Encoding")
    pat=re.compile(u"charset=(.*)\r+\n")
    g=pat.search(s)
    if g==None: encode="utf-8"
    else: encode=g.groups()[0]
    dec="%sDecoder"%contentEncode
    res=file1.read()
    if hasattr(__decoder,dec):
        res=getattr(__decoder, dec)(__decoder(),res)
    return res.decode(encode)
def getFileNameFromUrl(url):
    n=url.rfind(u'/')
    return url[n+1:]
def downloadFile(url,filename=None):
    req=urllib2.Request(url)
    h=urllib2.HTTPHandler(debuglevel=0)
    opener=urllib2.build_opener(h)
    file1=opener.open(req)
    if not os.path.exists('download'):       
        os.makedirs('download')
    if filename==None:filename=getFileNameFromUrl(url)
    f=open(r"download\%s"% filename,'wb')
    len1=file1.info().getheader("Content-Length")
    print u"开始下载:%s,大小:%s"%(filename,len1)
    while 1:
        k=file1.read(4096)
        if len(k)==0:
            break
        f.write(k)
    f.close()
    print u"下载完成:%s"%filename
def saveTxt(str,path):
    if type(str)==type(u''):str=str.encode('utf-8')
    f=open(path,'w')
    f.write(str)
    f.close()
if __name__=="__main__":
    #info("gzip")
    getHtml(u'http://bilibili.smgbb.cn/video/av8600/')
    #getHtml(u'http://www.baidu.com')
    #downloadFile(u'http://61.55.189.81/youku/677209B25F03B7DEDD6FA2685/03000801004ED4B4014429058D58186907EF17-527B-0499-3476-09FDC2D38E6A.mp4')
    #downloadFile(u'http://www.douban.com')
    print " "
    
    